Java in a Nutshell by Ben Evans

Java in a Nutshell by Ben Evans
Author: Ben Evans
Language: eng
Format: epub
Publisher: O'Reilly Media
Published: 2018-12-06

Don’t overburden a single exception type with describing too many conditions; for example, the Nashorn JavaScript implementation (new with Java 8) originally had overly coarse-grained exceptions, although this was fixed before release.

Finally, two exception-handling antipatterns that you should avoid:


// Never just swallow an exception try { someMethodThatMightThrow(); } catch(Exception e){ } // Never catch, log, and rethrow an exception try { someMethodThatMightThrow(); } catch(SpecificException e){ log(e); throw e; }

The former of these two just ignores a condition that almost certainly required some action (even if just a notification in a log). This increases the likelihood of failure elsewhere in the system—potentially far from the original, real source.

The second one just creates noise. We’re logging a message but not actually doing anything about the issue; we still require some other code higher up in the system to actually deal with the problem.